package com.weic.easycache.user;

import android.content.Context;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Expose;
import com.weic.easycache.framework.Config;
import com.weic.easycache.phone.Device;
import com.weic.easycache.phone.Network;
import com.weic.easycache.utils.Manifest;

public class DeviceInfo {
	@Expose
	public final String clientVer;
	@Expose
	public final String cpu;
	@Expose
	public final String sysVer;
	@Expose
	public final String brand;
	@Expose
	public final String macAddr;
	@Expose
	public final String devId;
	@Expose
	public String net;
	@Expose
	public String channel;

	private Context mContext;

	public static DeviceInfo mInstance;
	public static DeviceInfo getInstance(Context context) {
		if(mInstance == null) {
			mInstance = new DeviceInfo(context);
		}
		mInstance.mContext = context;
		return mInstance;
	}

	private DeviceInfo(Context context) {
		Device device = Device.getInstance(context);

		clientVer	= Config.get().versionName;
		cpu			= device.cpuAbi;
		sysVer		= device.sysVersion;
		brand		= device.brand;
		macAddr		= device.macAddress;
		devId		= device.getUniqueId(context);
		channel		= Manifest.MetaData.getString(context, "UMENG_CHANNEL");
	}

	public String toJson() {
		net	= Network.getConnectedNetworkType(mContext).type;
		Gson gson = new GsonBuilder()
		.excludeFieldsWithoutExposeAnnotation() //不导出实体中没有用@Expose注解的属性
		//.enableComplexMapKeySerialization() //支持Map的key为复杂对象的形式 
		.serializeNulls()
		.setDateFormat("yyyy-MM-dd HH:mm:ss:SSS")//时间转化为特定格式
		.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)//会把字段首字母大写
		.create();
		return gson.toJson(this);
	}
}
